﻿using LiteBlog.Common.Entities;
using LiteBlog.Components;
using LiteBlog.Components.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LiteBlog.Controllers.Filters
{
    public class ArchiveNotFoundFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            int year = (int)filterContext.ActionParameters["year"];
            int month = (int)filterContext.ActionParameters["month"];
            var controller = filterContext.Controller as ArchiveController;
            var archiveMonth = controller.ArchiveComponent.Get(month, year);

            if (archiveMonth == null)
            {
                filterContext.Result = controller.GetViewResult("PageNotFound", null);
                return;
            }

            filterContext.RouteData.Values["monthName"] = archiveMonth.Name;
        }
    }
}